import java.util.*;

public class Solution2 {
    //    //层序遍历
    public List<Double> averageOfLevels(TreeNode root) {
        Queue<TreeNode> queue=new LinkedList<>();
        List<Double> list=new ArrayList<>();
        if(root==null){
            return list;
        }
        queue.offer(root);

        while(!queue.isEmpty()){
            double sum=0;
            int size=queue.size();
            for(int i=0;i<size;i++){
                TreeNode cur=queue.poll();
                    sum+=cur.val;
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
            }
            list.add(sum/size);
        }
        return list;
    }

}
